<?php

namespace JsonLowCode\Traits\Events;

use JsonLowCode\Support\JsRouter;

trait HasRouterEvent
{
    /**
     * 返回上一级路由
     * @return $this
     */
    public function onBack(): static
    {
        return $this->onClick(JsRouter::make()->back());
    }

    /**
     * 前进一级路由
     * @return $this
     */
    public function onForward(): static
    {
        return $this->onClick(JsRouter::make()->forward());
    }

    /**
     * 跳转路由
     * @return $this
     */
    public function onPush(string|array $to): static
    {
        return $this->onClick(JsRouter::make()->push($to));
    }

    /**
     * 替换路由
     * @return $this
     */
    public function onReplace(string|array $to): static
    {
        return $this->onClick(JsRouter::make()->replace($to));
    }

    /**
     * 是否存在路由
     * @param string $name
     * @return $this
     */
    public function hasRoute(string $name): static
    {
        return $this->onClick(JsRouter::make()->hasRoute($name));
    }

    /**
     * 获取所有路由
     * @return $this
     */
    public function getRoutes(): static
    {
        return $this->onClick(JsRouter::make()->getRoutes());
    }

    /**
     * 是否存在路由
     * @return $this
     */
    public function currentRoute(): static
    {
        $currentRoute = JsRouter::make(false)->currentRoute();
        return $this->onClick(scriptFunc("
            var info = {$currentRoute};
            console.log(info.value);
        "));
    }
}
